package br.com.hstar.client.util;

import br.com.hstar.client.util.calendar.DateFormatter;
import br.com.hstar.client.util.calendar.DatePicker;
import br.com.hstar.client.util.calendar.SimpleDatePicker;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FormPanel extends VerticalPanel {
	
	private DockPanel dockPanel;
	private HorizontalPanel hp;
	private static FlexTable table;
	private HorizontalPanel buttonsPanel;
	private static int currentRow;
	private VerticalPanel left = new VerticalPanel();
	private VerticalPanel right = new VerticalPanel();
	
	public FormPanel(String _title) {
		super();
		this.dockPanel = new DockPanel();
		this.hp = new HorizontalPanel();
		
		// Creates HTML for the south bar, which may or may not contain
		// buttons or other elements (should be added later by a call
		// to the public interface. ).
		buttonsPanel = new HorizontalPanel();
		buttonsPanel.setStyleName( "Intpanel-South" );
		buttonsPanel.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_RIGHT );
		buttonsPanel.setHorizontalAlignment( HasAlignment.ALIGN_RIGHT );
		
		
		if ( _title != null ) {
			// creates html for the title at the top.
			HTML html = new HTML( _title );
			html.setStyleName( "IntPanel-Title" );
			dockPanel.add( html, DockPanel.NORTH );
		}
		
		dockPanel.add( buttonsPanel, DockPanel.SOUTH );
		dockPanel.setStyleName( "DockPanel" );
		
		// adds the center table where the elements go later.
		table = new FlexTable();
		table.setStyleName( "ContentTable" );
		dockPanel.add( table, DockPanel.CENTER );
		currentRow = 0;
		
		hp.add( left );
		hp.add( dockPanel );
		hp.add( right );
		
		add(hp);
	}
	
	public HorizontalPanel getHp(){
		return hp;
	}
 	
	public static TextBox addTextBox(String title, int maxLength, int visibleLength, String value) {
		TextBox text = new TextBox();
		text.setMaxLength(maxLength);
		text.setVisibleLength(visibleLength);
		text.setText(value);
		addWidget(title, text);
		return text;
	}
	
	public HorizontalPanel addHPanel(String title) {
		HorizontalPanel p = new HorizontalPanel();
		addWidget(title, p);
		return p;
	}
	
	public PasswordTextBox addPasswordTextBox(String title, String value) {
		PasswordTextBox text = new PasswordTextBox();
		text.setText(value);
		addWidget(title, text);
		return text;
	}
	
	public CheckBox addCheckBox(String title, boolean checked) {
		CheckBox check = new CheckBox();
		check.setChecked(checked);
		//check.setText(value);
		addWidget(title, check);
		return check;
	}
	
	public TextArea addTextArea(String title, String widith, String height, String value) {
		TextArea text = new TextArea();
		text.setSize(widith, height);
		text.setText(value);
		addWidget(title, text);
		return text; 
	}
	
	public PasswordTextBox addPassTextBox(String title, String value) {
		PasswordTextBox text = new PasswordTextBox();
		text.setText(value);
		addWidget(title, text);
		return text;
	}
		
	public DatePicker addCalendar(String title) {
		final DatePicker datePicker = new SimpleDatePicker("mydate");
	    // Set whether or not you want the weekends selectable
	    datePicker.setWeekendSelectable(true);
	    // Set the Date Format
	    datePicker.setDateFormat(DateFormatter.DATE_FORMAT_DDMONYYYY);
	    addWidget(title, datePicker);
	    return datePicker;
	}
	
	public static ListBox addListBox(String title, boolean acceptEmpty){
		ListBox listBox = new ListBox();
		if(acceptEmpty){
			listBox.addItem("");
		}
		listBox.setMultipleSelect(false);		
		listBox.setTitle(title);
		addWidget(title, listBox);
		return listBox;
	}
	
	public static ListBox createListBox(boolean acceptEmpty){
		ListBox listBox = new ListBox();
		if(acceptEmpty){
			listBox.addItem("");
		}
		listBox.setMultipleSelect(false);		
		//listBox.setTitle(title);
		//addWidget(title, listBox);
		return listBox;
	}
	
	public Button addButton(String text) {
		Button but = new Button(text);
		buttonsPanel.add(but);
		return but;
	}

	public static void addWidget(String title, Widget widget) {
		if(title == null || title.equals("")) {
			table.setWidget(currentRow++, 0, widget);
		} else {
			table.setWidget(currentRow, 0, new HTML(title));
			table.setWidget(currentRow++, 1, widget);
		}
	}
	
	public static String getImageUrl( String relImgUrl ) {
		return GWT.getModuleBaseURL() + relImgUrl;
	}

}
